/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package parser.serverData.npcTeleporter;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Collection;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;

import parser.clientData.clientAirline.AirlineList;
import parser.clientData.clientAirline.ClientAirline;
import parser.clientData.clientAirports.ClientAirport;
import parser.util.DataManager;

/**
 * @author Mr. Poke
 *
 */
public class NpcTeleporterSave
{
	public static void save()
	{
		ObjectFactory objFactory = new ObjectFactory();
		Teleports collection = objFactory.createTeleports();
		List<TeleportTemplate> templateList = collection.getTeleportTemplate();
		Collection<ClientAirline> airlineList = DataManager.getInstance().getClientAirline().values();

		for (ClientAirline airline : airlineList)
		{
			TeleportTemplate template = new TeleportTemplate();
			template.setTeleportId((int) airline.getId());
			AirlineList list = airline.getAirlineList();
			if (list == null)
				continue;
			List<parser.clientData.clientAirline.Data> datas = list.getData();
			if (datas == null || datas.isEmpty())
				continue;
			templateList.add(template);
			template.setType(TeleportType.REGULAR);
			for (parser.clientData.clientAirline.Data data : datas)
			{

				TeleportLocation loc = new TeleportLocation();
				loc.setPrice(data.getFee());
				if (data.getFlightPathGroupId() != null && data.getFlightPathGroupId() != 0)
				{
					template.setType(TeleportType.FLIGHT);
					loc.setTeleportid(data.getFlightPathGroupId() * 1000 + 1);
				}
				ClientAirport port = DataManager.getInstance().getClientAirports().get(data.getAirportName().toUpperCase());
				if (port == null)
					System.out.println("Missing AirPort: " + data.getAirportName());

				loc.setLocId(port.getId());
				template.getTelelocation().add(loc);
			}
		}
		try
		{
			JAXBContext jaxbContext = JAXBContext.newInstance("parser.serverData.npcTeleporter");
			Marshaller marshaller = jaxbContext.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
			marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, "teleports.xsd");
			marshaller.marshal(collection, new FileOutputStream("static_data/teleports/teleports.xml"));
		}
		catch (PropertyException e)
		{
			e.printStackTrace();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (JAXBException e)
		{
			e.printStackTrace();
		}
	}
}
